#include "editormanager.h"

using namespace core;

EditorManager* editorManager = 0;

EditorManager* EditorManager::getInstance()
{
    if (editorManager == 0)
    {
        editorManager = new EditorManager();
    }

    return editorManager;
}

void EditorManager::destroy()
{
    delete editorManager;
    editorManager = 0;
}

void EditorManager::registerEditor(const MimeType& type, IEditor* editor)
{
    if (!m_registeredEditors.contains(type))
    {
        m_registeredEditors[type] = editor->copy();
    }
}

void EditorManager::unregisterEditor(const MimeType& type)
{
    if (m_registeredEditors.contains(type))
    {
        IEditor* editor = m_registeredEditors[type];
        m_registeredEditors.remove(type);

        delete editor;
    }
}

QSharedPointer<IEditor> EditorManager::editor(const MimeType& type) const
{
    QSharedPointer<IEditor> ret;

    if (m_registeredEditors.contains(type))
    {
        ret = QSharedPointer<IEditor>(m_registeredEditors[type]->copy());
    }

    return ret;
}

EditorManager::EditorManager() :
    m_registeredEditors()
{

}

EditorManager::~EditorManager()
{
    foreach (IEditor* editor, m_registeredEditors)
    {
        delete editor;
    }
}
